﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using devtm.Configuration;
using System.Configuration;

namespace devtm.Editor.Configuration.Command
{
    public class CommandAddItem : ICommand
    {


        private IElementSection elementSection;
        private System.Configuration.ConfigurationElement configurationElement;
        
        private MethodInfo MethodRedo;
        private object itemRedo;
        
        private MethodInfo MethodUndo;
        private object itemUndo;

        private TypeChange Change;
        public CommandAddItem(IElementSection elementSection, System.Configuration.ConfigurationElement configurationElement, MethodInfo MethodRedo, object itemRedo, MethodInfo MethodUndo, object itemUndo, TypeChange change)
        {

            Change = change;
            this.elementSection = elementSection;
            this.configurationElement = configurationElement;

            this.MethodRedo = MethodRedo;
            this.itemRedo = itemRedo;
            
            this.MethodUndo = MethodUndo;
            this.itemUndo = itemUndo;

        }

        public void Undo()
        {


            if (Change == TypeChange.Add)
                (elementSection.ParentItem as ElementSection).Items.Remove(elementSection);
            else
                (elementSection.ParentItem as ElementSection).Items.Add(elementSection);


            if (itemUndo is System.Configuration.ConnectionStringSettings)
            {
                if (Change == TypeChange.Add)
                    (configurationElement as ConnectionStringSettingsCollection).Remove((itemUndo as System.Configuration.ConnectionStringSettings));
                else
                    (configurationElement as ConnectionStringSettingsCollection).Add((itemUndo as System.Configuration.ConnectionStringSettings));


            }
            else if (itemUndo is System.Configuration.KeyValueConfigurationElement)
            {

                if (Change == TypeChange.Add)
                (configurationElement as KeyValueConfigurationCollection).Remove((itemUndo as System.Configuration.KeyValueConfigurationElement).Key);
                else
                    (configurationElement as KeyValueConfigurationCollection).Add((itemUndo as System.Configuration.KeyValueConfigurationElement));
                
            }
            else if (itemUndo is MethodInfo)
            {

                string j = (string)(itemUndo as MethodInfo).Invoke(configurationElement, new object[] { itemRedo });
                MethodUndo.Invoke(configurationElement, new object[] { j });

            }
            else
                MethodUndo.Invoke(configurationElement, new object[] { itemUndo });

            

        }

        public void Redo()
        {

            if (Change == TypeChange.Add)
                (elementSection.ParentItem as ElementSection).Items.Add(elementSection);
            else
                (elementSection.ParentItem as ElementSection).Items.Remove(elementSection);


            MethodRedo.Invoke(configurationElement, new object[] { itemRedo });

        }


    }
}
